package com.order.model;  

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSONObject;
import com.jfinal.ext.plugin.tablebind.TableBind;
import com.jfinal.kit.StringKit;
import com.jfinal.plugin.activerecord.Model;
import com.jfinal.plugin.activerecord.Page;
import com.order.shiro.ShiroUtils;
import com.order.utils.DateUtils;
import com.order.utils.JsonUtils;

/** 
 * @Title: Cuisine.java 
 * @Package com.order.model 
 * @Description: 菜系类
 * @author LinBo
 * @date 2014-3-25 下午11:42:34 
 */

@TableBind(tableName="cuisine", pkName="id")
public class Cuisine extends Model<Cuisine> {

	/**
	 * 对应的菜品
	 */
	private List<Dish> dishs = new ArrayList<Dish>();
	
	public List<Dish> getDishs() {
		return dishs;
	}

	public void setDishs(List<Dish> dishs) {
		this.dishs = dishs;
	}

	/** 
	 * @Fields serialVersionUID : 当前类可序列化的版本
	 */  
	private static final long serialVersionUID = -4192804283974540325L;
	public static final String ID = "id";
	public static final String NAME = "name";
	public static final String STATUS = "status";
	public static final String INFO = "info";
	public static final String LAST_UPDATED_BY = "last_updated_by";
	public static final String LAST_UPDATE_DATE = "last_update_date";
	public static final Cuisine dao = new Cuisine();


	public Map<String, Object> getCuisineJson(int page, int pagesize,JSONObject jsonObj) {
		String select = "select c.id, c.name, c.info, c.status, c.last_update_date,c.last_updated_by ";
		String sqlExceptSelect = "from cuisine c where 1=1 ";

		// 菜系名
		if (StringKit.notBlank(jsonObj.getString("NAME"))) {
			String name = jsonObj.getString("NAME");
			sqlExceptSelect += "  and c.name like '%" + name + "%'";
		}
		// 状态
		if (StringKit.notBlank(jsonObj.getString("STATUS"))) {
			String status = jsonObj.getString("STATUS");
			sqlExceptSelect += "  and c.status like '%" + status + "%'";
		}
		/*// 修改人
		if (StringKit.notBlank(jsonObj.getString("LAST_UPDATED_USRE"))) {
			String lastUpdatedUser = jsonObj.getString("LAST_UPDATED_USRE");
			sqlExceptSelect += "  and c.last_updated_by in( "
					+ "select u.id where u.name like '%" + lastUpdatedUser + "%')";
		}*/
		// 修改时间
		if (StringKit.notBlank(jsonObj.getString("LAST_UPDATE_DATE"))) {
			String lastUpdateDate = jsonObj.getString("LAST_UPDATE_DATE");
			sqlExceptSelect += "  and c.last_update_date like '%" + lastUpdateDate.substring(0, 10) + "%'";
		}
		
		Page<Cuisine> list = dao.paginate(page, pagesize, select,sqlExceptSelect);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("Rows", list.getList());
		map.put("Total", list.getTotalRow());
		return map;
	}
	
	public Cuisine getObj(String json) {
		try {
			return setAttrs(JsonUtils.fastjson2Map(json));
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		return this;
	}
	
	/**保存*/
	public boolean mySave() {
		set(ID, null);
		set(NAME, get(NAME));
		set(INFO, get(INFO));
		set(LAST_UPDATED_BY, ShiroUtils.getUserId());
		set(LAST_UPDATE_DATE, DateUtils.getNowTime());
		return save();
	}
	
	/**更新*/
	public boolean myUpdate() {
		set(ID, get(ID));
		set(NAME, get(NAME));
		set(INFO, get(INFO));
		set(LAST_UPDATED_BY, ShiroUtils.getUserId());
		set(LAST_UPDATE_DATE, DateUtils.getNowTime());
		return update();
	}

	/** 
	* @描述: 获取菜系树
	*/ 
	public String getCuisineTree() {
		StringBuffer json = new StringBuffer("[{\"cuisine_id\":\"0\",\"text\":\"所有菜系\",\"children\":["); // 初始化根节点
		String sql = "select c.id, c.name from cuisine c ";
		List<Cuisine> cuisines = Cuisine.dao.find(sql);
		StringBuffer parent_json = new StringBuffer();
		if (cuisines != null && cuisines.size() > 0) {
			for (int i = 0; i < cuisines.size(); i++) {
				parent_json.append("{\"cuisine_id\":\""+ cuisines.get(i).getNumber("ID").intValue()+ "\",");
				parent_json.append("\"text\":\""+ cuisines.get(i).getStr("NAME").toString().trim()+ "\"");
				parent_json.append("},");
			}
		} else {
			parent_json.append("},");
		}
		json.append(parent_json.toString());
		json = new StringBuffer(json.substring(0, json.length() - 1) + "]}]");
		return json.toString();
	}

	/** 
	* @Title: getCuisineDish 
	* @描述: 获取所有菜系、菜品信息
	* @return   
	*/ 
	public List<Cuisine> getCuisineDish() {
		String selectCuisine = " select c.id, c.name, c.info "
				+ " from cuisine c "
				+ " where c.status <> 0 ";
		List<Cuisine> list = dao.find(selectCuisine);
		for(int i = 0; i<list.size(); i++){
			String selectDish = "select d.id dish_id, cd.id cuisine_dish_id, d.name, "
					+ " d.sq, d.info, d.price, d.image, d.status, d.viewcount "
					+ " from dish d, cuisine_dish cd "
					+ " where cd.cuisine_id = ? "
					+ " and cd.dish_id = d.id "
					+ " and d.status <> 0 "
					+ " order by d.status desc, d.sq asc, d.status asc ";
			List<Dish> DishList = Dish.dao.find(selectDish, new Object[]{list.get(i).get(Cuisine.ID)});
			list.get(i).setDishs(DishList);
		}
		return list;
	}
}
  